
package doc2db.interfaces.mainWindow;

import doc2db.Keey;
import doc2db.Launcher;
import doc2db.UserProp;
import doc2db.fileWorker.FileBeginer;
import doc2db.interfaces.action.templates.ChoiceTemplAction;
import doc2db.interfaces.action.CloseConectionAction;
import doc2db.interfaces.action.templates.CorectTemplatesAction;
import doc2db.interfaces.action.CreateConectionAction;
import doc2db.interfaces.action.DelTableAction;
import doc2db.interfaces.action.InsUpd.InsertAction;
import doc2db.interfaces.action.templates.NewTempletAction;
import doc2db.interfaces.action.ShowTableAction;
import doc2db.interfaces.action.TableListAction;
import doc2db.interfaces.action.InsUpd.UpdateAction;
import doc2db.interfaces.action.ToTreangleTabAction;
import doc2db.interfaces.action.templates.corectTemplInDbAction;
import doc2db.interfaces.action.createTable.CreateTableAction;
import doc2db.interfaces.action.open.OpenDir;
import doc2db.interfaces.action.open.OpenFile;
import doc2db.interfaces.action.save.SaveAction;
import doc2db.interfaces.action.save.SaveAsAction;
import doc2db.interfaces.mainWindow.action.MyMouseListener;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.event.MouseListener;
import java.io.File;
import javax.swing.Box;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JEditorPane;
import javax.swing.JToolBar;
import javax.swing.SwingConstants;

/**
 *
 * @author yuriy
 */
public class MyToolBar extends JToolBar{
    private MyFrame mf;
    private Font fon = UserProp.paneFont;
    
    /** Thet button looklike buti (withoute border, but bordering when mouse over) */
    private MouseListener rolloverButtonListener = new MyMouseListener();
    
    /**
     * full path to icons <ol>
     * <li>view-restore</li>    <li>tabl</li>         <li>delete</li>
     * <li>createMyCon</li>     <li>createNewCon</li> <li>closeCon</li>
     * <li>createTempl</li>     <li>choiceTempl</li>  <li>corectTempl</li>
     * <li>corectTemplInDb</li> <li>insert</li>       <li>update</li>
     * <li>createTable</li>     <li>modetabl</li>     <li>open</li>
     * <li>openDir</li>         <li>save</li>         <li>saveAs</li>
     * </ol>
     */
    private static final String picturePath [] ;
    
    /** init icons path array */
    static {
        final String path = FileBeginer.resurse_resurseFolderTolBar + Keey.sp;
        picturePath = new String[]{
                    path + "view-restore.png",
                    path + "tabl.png",
                    path + "edit-delete_003.png",
                    path + "createMyCon.png",
                    path + "createNewCon.png",
                    path + "closeCon.png",
                    path + "createTempl.png",
                    path + "choiceTempl.png",
                    path + "corectTempl.png",
                    path + "corectTemplInDb.png",
                    path + "insert.png",
                    path + "update.png",
                    path + "createTable.png",
                    path + "modetabl.png",
                    path + "open.png",
                    path + "openfolder.png",
                    path + "save.png",
                    path + "save-as.png"
                };
    };
// <editor-fold defaultstate="collapsed" desc="component">
    public JButton tablList = new JButton();
    public JButton tabl = new JButton();
    public JButton delTabl = new JButton();
    public JButton insert = new JButton();
    public JButton update = new JButton();
    public JButton modetab = new JButton();
    public JButton createTable = new JButton();
    public JButton createMyCon = new JButton();
    public JButton createNewCon = new JButton();
    public JButton closeCon = new JButton();
    public JButton createTempl = new JButton();
    public JButton choiceTempl = new JButton();
    public JButton corectTempl = new JButton();
    public JButton corectTemplInDb = new JButton();
    public JButton open = new JButton();
    public JButton openDir = new JButton();
    public JButton save = new JButton();
    public JButton saveAs = new JButton();
    public VMemory vmem = new VMemory();
    public Component strukttablList = Box.createHorizontalStrut(10);
    public Component strukttabl = Box.createHorizontalStrut(10);
    public Component struktdelTabl = Box.createHorizontalStrut(10);
    public Component struktinsert = Box.createHorizontalStrut(10);
    public Component struktupdate = Box.createHorizontalStrut(10);
    public Component struktmodetab = Box.createHorizontalStrut(10);
    public Component struktcreateTable = Box.createHorizontalStrut(10);
    public Component struktcreateMyCon = Box.createHorizontalStrut(10);
    public Component struktcreateNewCon = Box.createHorizontalStrut(10);
    public Component struktcloseCon = Box.createHorizontalStrut(10);
    public Component struktcreateTempl = Box.createHorizontalStrut(10);
    public Component struktchoiceTempl = Box.createHorizontalStrut(10);
    public Component struktcorectTempl = Box.createHorizontalStrut(10);
    public Component struktcorectTemplInDb = Box.createHorizontalStrut(10);
    public Component strukopen = Box.createHorizontalStrut(10);
    public Component strukopenDir = Box.createHorizontalStrut(10);
    public Component struksave = Box.createHorizontalStrut(10);
    public Component struksaveAs = Box.createHorizontalStrut(10);
    // </editor-fold>
    
    public MyToolBar(MyFrame m) {
        mf = m;
        this.initVar();
        this.initComp();
    }

    private void initComp() {
        Box b = Box.createHorizontalBox();
        b.add(Box.createHorizontalStrut(10));
        b.add(open);
        b.add(strukopen);
        b.add(openDir);
        b.add(strukopenDir);
        b.add(save);
        b.add(struksave);
        b.add(saveAs);
        b.add(struksaveAs);
        b.add(tablList);
        b.add(strukttablList);
        b.add(tabl);
        b.add(strukttabl);
        b.add(delTabl);
        b.add(struktdelTabl);
        b.add(insert);
        b.add(struktinsert);
        b.add(update);
        b.add(struktupdate);
        b.add(createTable);
        b.add(struktcreateTable);
        b.add(modetab);
        b.add(struktmodetab);
        b.add(createMyCon);
        b.add(struktcreateMyCon);
        b.add(createNewCon);
        b.add(struktcreateNewCon);
        b.add(closeCon);
        b.add(struktcloseCon);
        b.add(createTempl);
        b.add(struktcreateTempl);
        b.add(choiceTempl);
        b.add(struktchoiceTempl);
        b.add(corectTempl);
        b.add(struktcorectTempl);
        b.add(corectTemplInDb);
        b.add(struktcorectTemplInDb);
        b.add(Box.createHorizontalStrut(15));
        b.add(Box.createGlue());
        b.add(vmem);
        add(b);
        setFloatable(false); // запрещаем двигать панель
        setOrientation(SwingConstants.HORIZONTAL);// установить ориентацию панели
    }

    private void initVar() {
        this.initVarSize();
        this.initVarAction();
    }

    private void initVarAction() {
        MyJEditorPane view = mf.pane.textWeekendInformation;
        JEditorPane dbOutput = mf.pane.textDBSelects;
        
        open.addActionListener(new OpenFile(mf, mf.pane));
        openDir.addActionListener(new OpenDir(mf, mf.pane));
        save.addActionListener(new SaveAction(view));
        saveAs.addActionListener(new SaveAsAction(view));
        
        createMyCon.addActionListener(new CreateConectionAction(mf, false));
        createNewCon.addActionListener(new CreateConectionAction(mf, true));
        closeCon.addActionListener(new CloseConectionAction(mf));
        tablList.addActionListener(new TableListAction(view,dbOutput));
        tabl.addActionListener(new ShowTableAction(view,dbOutput));
        delTabl.addActionListener(new DelTableAction( view,dbOutput));
        insert.addActionListener(new InsertAction(view,dbOutput));
        update.addActionListener(new UpdateAction(view,dbOutput));
        modetab.addActionListener(new ToTreangleTabAction(view,dbOutput));
        createTable.addActionListener(new CreateTableAction(dbOutput,view));
        createTempl.addActionListener(new NewTempletAction());
        ChoiceTemplAction a = new ChoiceTemplAction(null, null);
        a.setTextArea(mf.pane.textTemp, mf.pane.textMetaTemp);
        choiceTempl.addActionListener(a);

        corectTempl.addActionListener(new CorectTemplatesAction());
        corectTemplInDb.addActionListener(new corectTemplInDbAction(null, null));
    }

    private void initVarSize() {
        this.initVarSizeFont();
        this.initVarSizeButon(32);
        this.initVarSizeToolTipText();
        this.initVarSizeIcon();
        this.initVarSizeRolloverButton();

        boolean h = false;  //hide
        boolean v = true;   //visible
        open.setEnabled(v);
        open.setVisible(UserProp.open);
        strukopen.setVisible(UserProp.open);
        openDir.setEnabled(v);
        openDir.setVisible(UserProp.openDir);
        strukopenDir.setVisible(UserProp.openDir);
        save.setEnabled(v);
        save.setVisible(UserProp.save);
        struksave.setVisible(UserProp.save);
        saveAs.setEnabled(v);
        saveAs.setVisible(UserProp.saveAs);
        struksaveAs.setVisible(UserProp.saveAs);

        tablList.setEnabled(h);
        tablList.setVisible(UserProp.tableList);
        strukttablList.setVisible(UserProp.tableList);
        tabl.setEnabled(h);
        tabl.setVisible(UserProp.showTable);
        strukttabl.setVisible(UserProp.showTable);
        delTabl.setEnabled(h);
        delTabl.setVisible(UserProp.delTable);
        struktdelTabl.setVisible(UserProp.delTable);
        insert.setEnabled(h);
        insert.setVisible(UserProp.insert);
        struktinsert.setVisible(UserProp.insert);
        update.setEnabled(h);
        update.setVisible(UserProp.update);
        struktupdate.setVisible(UserProp.update);
        modetab.setEnabled(h);
        modetab.setVisible(UserProp.modetab);
        struktmodetab.setVisible(UserProp.modetab);
        createTable.setEnabled(h);
        createTable.setVisible(UserProp.createTable);
        struktcreateTable.setVisible(UserProp.createTable);
        createMyCon.setEnabled(v);
        createMyCon.setVisible(UserProp.createMyCon);
        struktcreateMyCon.setVisible(UserProp.createMyCon);
        createNewCon.setEnabled(v);
        createNewCon.setVisible(UserProp.createNewCon);
        struktcreateNewCon.setVisible(UserProp.createNewCon);
        closeCon.setEnabled(h);
        closeCon.setVisible(UserProp.closeCon);
        struktcloseCon.setVisible(UserProp.closeCon);
        createTempl.setEnabled(v);
        createTempl.setVisible(UserProp.createTempl);
        struktcreateTempl.setVisible(UserProp.createTempl);
        choiceTempl.setEnabled(v);
        choiceTempl.setVisible(UserProp.choiceTempl);
        struktchoiceTempl.setVisible(UserProp.choiceTempl);
        corectTempl.setEnabled(v);
        corectTempl.setVisible(UserProp.corectTempl);
        struktcorectTempl.setVisible(UserProp.corectTempl);
        corectTemplInDb.setEnabled(v);
        corectTemplInDb.setVisible(UserProp.corectTempl);
        struktcorectTemplInDb.setVisible(UserProp.corectTempl);
        vmem.setVisible(Keey.debug);
        setVisible(UserProp.seeAll);
    }

    public void setEnableButton(boolean b){
        save.setEnabled(b);
        saveAs.setEnabled(b);
        open.setEnabled(b);
        openDir.setEnabled(b);
        tablList.setEnabled(b);
        tabl.setEnabled(b);
        delTabl.setEnabled(b);
        insert.setEnabled(b);
        update.setEnabled(b);
        modetab.setEnabled(b);
        createTable.setEnabled(b);
        closeCon.setEnabled(b);
    }

    private void initVarSizeButon(int i) {
        Dimension butonSize = new Dimension(i, i);
        save.setPreferredSize(butonSize);
        saveAs.setPreferredSize(butonSize);
        open.setPreferredSize(butonSize);
        openDir.setPreferredSize(butonSize);

        tablList.setPreferredSize(butonSize);
        tabl.setPreferredSize(butonSize);
        delTabl.setPreferredSize(butonSize);
        insert.setPreferredSize(butonSize);
        update.setPreferredSize(butonSize);
        modetab.setPreferredSize(butonSize);
        createTable.setPreferredSize(butonSize);
        createMyCon.setPreferredSize(butonSize);
        createNewCon.setPreferredSize(butonSize);
        closeCon.setPreferredSize(butonSize);
        createTempl.setPreferredSize(butonSize);
        choiceTempl.setPreferredSize(butonSize);
        corectTempl.setPreferredSize(butonSize);
        corectTemplInDb.setPreferredSize(butonSize);
    }

    private void initVarSizeFont() {
        save.setFont(fon);
        saveAs.setFont(fon);
        open.setFont(fon);
        openDir.setFont(fon);
        tablList.setFont(fon);
        tabl.setFont(fon);
        delTabl.setFont(fon);
        insert.setFont(fon);
        update.setFont(fon);
        modetab.setFont(fon);
        createTable.setFont(fon);
        createMyCon.setFont(fon);
        createNewCon.setFont(fon);
        closeCon.setFont(fon);
        createTempl.setFont(fon);
        choiceTempl.setFont(fon);
        corectTempl.setFont(fon);
        corectTemplInDb.setFont(fon);
    }

    private void initVarSizeIcon() {
        // <editor-fold defaultstate="collapsed" desc="set icons from file">
        tablList.setIcon(new ImageIcon(picturePath[0]));
        tabl.setIcon(new ImageIcon(picturePath[1]));
        delTabl.setIcon(new ImageIcon(picturePath[2]));
        createMyCon.setIcon(new ImageIcon(picturePath[3]));
        createNewCon.setIcon(new ImageIcon(picturePath[4]));
        closeCon.setIcon(new ImageIcon(picturePath[5]));
        createTempl.setIcon(new ImageIcon(picturePath[6]));
        choiceTempl.setIcon(new ImageIcon(picturePath[7]));
        corectTempl.setIcon(new ImageIcon(picturePath[8]));
        corectTemplInDb.setIcon(new ImageIcon(picturePath[9]));
        insert.setIcon(new ImageIcon(picturePath[10]));
        update.setIcon(new ImageIcon(picturePath[11]));
        createTable.setIcon(new ImageIcon(picturePath[12]));
        modetab.setIcon(new ImageIcon(picturePath[13]));
        open.setIcon(new ImageIcon(picturePath[14]));
        openDir.setIcon(new ImageIcon(picturePath[15]));
        save.setIcon(new ImageIcon(picturePath[16]));
        saveAs.setIcon(new ImageIcon(picturePath[17]));
        // </editor-fold>

        String path = "tolBar/";
        if (! new File(picturePath[0]).exists()) {
            tablList.setIcon(Launcher.getIcon(path + "view-restore.png"));
        }
        if (!new File(picturePath[1]).exists()) {
            tabl.setIcon(Launcher.getIcon(path + "tabl.png"));
        }
        if (!new File(picturePath[2]).exists()) {
            delTabl.setIcon(Launcher.getIcon(path + "edit-delete_003.png"));
        }
        if (!new File(picturePath[3]).exists()) {
            createMyCon.setIcon(Launcher.getIcon(path + "createMyCon.png"));
        }
        if (!new File(picturePath[4]).exists()) {
            createNewCon.setIcon(Launcher.getIcon(path + "createNewCon.png"));
        }
        if (!new File(picturePath[5]).exists()) {
            closeCon.setIcon(Launcher.getIcon(path + "closeCon.png"));
        }
        if (!new File(picturePath[6]).exists()) {
            createTempl.setIcon(Launcher.getIcon(path + "createTempl.png"));
        }
        if (!new File(picturePath[7]).exists()) {
            choiceTempl.setIcon(Launcher.getIcon(path + "choiceTempl.png"));
        }
        if (!new File(picturePath[8]).exists()) {
            corectTempl.setIcon(Launcher.getIcon(path + "corectTempl.png"));
        }
        if (!new File(picturePath[9]).exists()) {
            corectTemplInDb.setIcon(Launcher.getIcon(path + "corectTemplInDb.png"));
        }
        if (!new File(picturePath[10]).exists()) {
            insert.setIcon(Launcher.getIcon(path + "insert.png"));
        }
        if (!new File(picturePath[11]).exists()) {
            update.setIcon(Launcher.getIcon(path + "update.png"));
        }
        if (!new File(picturePath[12]).exists()) {
            createTable.setIcon(Launcher.getIcon(path + "createTable.png"));
        }
        if (!new File(picturePath[13]).exists()) {
            modetab.setIcon(Launcher.getIcon(path + "modetabl.png"));
        }
        if (!new File(picturePath[14]).exists()) {
            open.setIcon(Launcher.getIcon(path + "open.png"));
        }
        if (!new File(picturePath[15]).exists()) {
            openDir.setIcon(Launcher.getIcon(path + "openfolder.png"));
        }
        if (!new File(picturePath[16]).exists()) {
            save.setIcon(Launcher.getIcon(path + "save.png"));
        }
        if (!new File(picturePath[17]).exists()) {
            saveAs.setIcon(Launcher.getIcon(path + "save-as.png"));
        }
    }

    private void initVarSizeToolTipText() {
        String str = "<html>";
                //"<html><table bgcolor=yellow border=1><tr><th><font size=\"4\" face=\"Verdana\">";
        tablList.setToolTipText(str + "вывести список таблиц");
        tabl.setToolTipText(str + "вывести таблицу");
        delTabl.setToolTipText(str + "удалить таблицу");
        insert.setToolTipText(str + "дополнить таблицу, тоесть <br>" +
                                    "внести новые данные");
        update.setToolTipText(str + "обновить таблицу, тоесть <br>" +
                                    "старые данные на новые с <br>" +
                                    "определённым соответствием");
        modetab.setToolTipText(str + "преобразовывает таблицу" +
                                    "определённым соответствием");
        createTable.setToolTipText(str + "Создать таблицу");
        createMyCon.setToolTipText(str + "создать соеденение, <br>" +
                                         "используя параметры <br>" +
                                         "пользователя, как в <br>" +
                                         "данной системе");
        createNewCon.setToolTipText(str +"создать соеденение, <br>" +
                                         "используя другие <br>" +
                                         "параметры пользователя");
        closeCon.setToolTipText(str + "закрыть соеденение с Б.Д.");
        createTempl.setToolTipText(str + "создать новый шабон");
        choiceTempl.setToolTipText(str + "выбрать существующий шаблон");
        corectTempl.setToolTipText(str + "Настроить шаблон");
        corectTemplInDb.setToolTipText(str + "Настроить шаблон в БД");
        open.setToolTipText(str + "Открыть файл");
        openDir.setToolTipText(str + "Открыть папку");
        save.setToolTipText(str + "Сохранить в html");
        saveAs.setToolTipText(str + "Сохранить как ...");
    }
    
    
    private void initVarSizeRolloverButton(){
        final boolean car = false;
        tablList.setContentAreaFilled(car);
        tablList.addMouseListener(rolloverButtonListener);
        tabl.setContentAreaFilled(car);
        tabl.addMouseListener(rolloverButtonListener);
        delTabl.setContentAreaFilled(car);
        delTabl.addMouseListener(rolloverButtonListener);
        insert.setContentAreaFilled(car);
        insert.addMouseListener(rolloverButtonListener);
        update.setContentAreaFilled(car);
        update.addMouseListener(rolloverButtonListener);
        modetab.setContentAreaFilled(car);
        modetab.addMouseListener(rolloverButtonListener);
        createTable.setContentAreaFilled(car);
        createTable.addMouseListener(rolloverButtonListener);
        createMyCon.setContentAreaFilled(car);
        createMyCon.addMouseListener(rolloverButtonListener);
        createNewCon.setContentAreaFilled(car);
        createNewCon.addMouseListener(rolloverButtonListener);
        closeCon.setContentAreaFilled(car);
        closeCon.addMouseListener(rolloverButtonListener);
        createTempl.setContentAreaFilled(car);
        createTempl.addMouseListener(rolloverButtonListener);
        choiceTempl.setContentAreaFilled(car);
        choiceTempl.addMouseListener(rolloverButtonListener);
        corectTempl.setContentAreaFilled(car);
        corectTempl.addMouseListener(rolloverButtonListener);
        corectTemplInDb.setContentAreaFilled(car);
        corectTemplInDb.addMouseListener(rolloverButtonListener);

        open.setContentAreaFilled(car);
        open.addMouseListener(rolloverButtonListener);
        openDir.setContentAreaFilled(car);
        openDir.addMouseListener(rolloverButtonListener);
        save.setContentAreaFilled(car);
        save.addMouseListener(rolloverButtonListener);
        saveAs.setContentAreaFilled(car);
        saveAs.addMouseListener(rolloverButtonListener);
    }


}
